package com.cloud.pole.shop.controller.user;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.shop.controller.IndexController;
import com.cloud.pole.shop.model.entity.user.ShopUserDeliAddr;
import com.cloud.pole.shop.model.vo.IndexVo;
import com.cloud.pole.shop.model.vo.goods.GoodsDetailVo;
import com.cloud.pole.shop.model.vo.order.ShoppingCartVo;
import com.cloud.pole.shop.service.IndexService;
import com.cloud.pole.shop.service.goods.GoodsService;
import com.cloud.pole.shop.service.order.OrderService;
import com.cloud.pole.shop.service.order.ShoppingCartService;
import com.cloud.pole.shop.service.user.UserDeliAddrService;

import lombok.extern.slf4j.Slf4j;

/*
*2019年2月21日
*cloud-shop
*author:frank-chen
*/
@Controller
@Slf4j
@RequestMapping("/userDeliAddr/front")
public class UserDeliAddrController {

	@Autowired
	private UserDeliAddrService userDeliAddrService;
	
	@ResponseBody
	@RequestMapping("/addDeliAddr")
    public ApiRestResponse<Integer> addDeliAddr(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String name,
    		@RequestParam(defaultValue = "") String tel,
    		@RequestParam(defaultValue = "") String proId,
    		@RequestParam(defaultValue = "") String cityId,
    		@RequestParam(defaultValue = "") String districtId,
    		@RequestParam(defaultValue = "") String proName,
    		@RequestParam(defaultValue = "") String cityName,
    		@RequestParam(defaultValue = "") String districtName,    		
    		@RequestParam(defaultValue = "") String address,
    		@RequestParam(defaultValue = "0") String defaultTag){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(name), "name不能为空");
			ParamException.err(StringUtils.isBlank(tel), "tel不能为空");
			ParamException.err(StringUtils.isBlank(proId), "proId不能为空");
			ParamException.err(StringUtils.isBlank(cityId), "cityId不能为空");
			ParamException.err(StringUtils.isBlank(districtId), "districtId不能为空");
			ParamException.err(StringUtils.isBlank(proName), "proName不能为空");
			ParamException.err(StringUtils.isBlank(cityName), "cityName不能为空");
			ParamException.err(StringUtils.isBlank(districtName), "districtName不能为空");
			ParamException.err(StringUtils.isBlank(address), "address不能为空");
			ParamException.err(!RegexUtils.isInteger(defaultTag), "defaultTag错误");
			Integer deliAddrId = userDeliAddrService.addDeliAddr(token, name, tel, proId, cityId, districtId, 
					proName, cityName, districtName, address, Integer.parseInt(defaultTag));
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, deliAddrId);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	//查询用户收货地址
	@ResponseBody
	@RequestMapping("/getDeliAddr")
    public ApiRestResponse<ShopUserDeliAddr> getDeliAddr(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String addrId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(addrId), "addrId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(addrId), "addrId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userDeliAddrService.getDeliAddr(token,  Integer.parseInt(addrId)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	//查询用户默认收货地址
	@ResponseBody
	@RequestMapping("/getDefaultDeliAddr")
    public ApiRestResponse<ShopUserDeliAddr> getDefaultDeliAddr(
    		@RequestHeader(defaultValue = "") String token){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userDeliAddrService.getDefaultDeliAddr(token));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	//设置用户默认收货地址
	@ResponseBody
	@RequestMapping("/setDefaultDeliAddr")
    public ApiRestResponse<Boolean> setDefaultDeliAddr(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String addrId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    
			ParamException.err(StringUtils.isBlank(addrId), "addrId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(addrId), "addrId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userDeliAddrService.setDefaultDeliAddr(token, Integer.parseInt(addrId)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	//修改用户收货地址
	@ResponseBody
	@RequestMapping("/modifyDeliAddr")
    public ApiRestResponse<Boolean> modifyDeliAddr(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String addrId,
    		@RequestParam(defaultValue = "") String name,
    		@RequestParam(defaultValue = "") String tel,
    		@RequestParam(defaultValue = "") String proId,
    		@RequestParam(defaultValue = "") String cityId,
    		@RequestParam(defaultValue = "") String districtId,
    		@RequestParam(defaultValue = "") String proName,
    		@RequestParam(defaultValue = "") String cityName,
    		@RequestParam(defaultValue = "") String districtName,    		
    		@RequestParam(defaultValue = "") String address,
    		@RequestParam(defaultValue = "0") String defaultTag){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    
			ParamException.err(StringUtils.isBlank(addrId), "addrId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(addrId), "addrId错误");

			ParamException.err(StringUtils.isBlank(name), "name不能为空");
			ParamException.err(StringUtils.isBlank(tel), "tel不能为空");
			ParamException.err(StringUtils.isBlank(proId), "proId不能为空");
			ParamException.err(StringUtils.isBlank(cityId), "cityId不能为空");
			ParamException.err(StringUtils.isBlank(districtId), "districtId不能为空");
			ParamException.err(StringUtils.isBlank(proName), "proName不能为空");
			ParamException.err(StringUtils.isBlank(cityName), "cityName不能为空");
			ParamException.err(StringUtils.isBlank(districtName), "districtName不能为空");
			ParamException.err(StringUtils.isBlank(address), "address不能为空");
			ParamException.err(!RegexUtils.isInteger(defaultTag), "defaultTag错误");
			boolean status = userDeliAddrService.modifyDeliAddr(token, Integer.parseInt(addrId), name, tel, proId, cityId, districtId, 
					proName, cityName, districtName, address, Integer.parseInt(defaultTag));
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, status);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	//删除用户收货地址
	@ResponseBody
	@RequestMapping("/delDeliAddr")
    public ApiRestResponse<Boolean> delDeliAddr(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String addrId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(addrId), "addrId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(addrId), "addrId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userDeliAddrService.delDeliAddr(token,  Integer.parseInt(addrId)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	//收货地址列表
	@ResponseBody
	@RequestMapping("/getDeliAddrList")
    public ApiRestResponse<List<ShopUserDeliAddr>> getDeliAddrList(
    		@RequestHeader(defaultValue = "") String token){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userDeliAddrService.getDeliAddrList(token));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
}
